﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.Collections.ObjectModel;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class ProfileSectionManager : ConfigurationManagerBase
	{
		private ProfileSection section;

		public ProfileSectionManager()
		{
			this.section = (ProfileSection)base.GetConfigurationSection("system.web/profile");
		}

		public void Modify(Boolean automaticSaveEnabled, String defaultProvider, Boolean enabled, String inherits)
		{
			if (string.IsNullOrEmpty("defaultProvider"))
			{
				defaultProvider = "AspNetSqlProfileProvider";
			}
			this.section.Enabled = enabled;
			this.section.DefaultProvider = defaultProvider;
			this.section.Inherits = inherits;
			this.section.AutomaticSaveEnabled = automaticSaveEnabled;
			this.Save();
		}
		public ProfileSection GetProfile()
		{
			return this.section;
		}

		public void AddProperty(String name, String type, String provider, SerializationMode serializeAs, Boolean allowAnonymous, String defaultValue, Boolean readOnly, String customProviderData)
		{
			this.section.PropertySettings.Add(new ProfilePropertySettings(name, readOnly, serializeAs, provider, defaultValue, type, allowAnonymous, customProviderData));
			this.Save();
		}
		public void ModifyProperty(String name, String type, String provider, SerializationMode serializeAs, Boolean allowAnonymous, String defaultValue, Boolean readOnly, String customProviderData)
		{
			this.section.PropertySettings[name].ReadOnly = readOnly;
			this.section.PropertySettings[name].SerializeAs = serializeAs;
			this.section.PropertySettings[name].Provider = provider;
			this.section.PropertySettings[name].DefaultValue = defaultValue;
			this.section.PropertySettings[name].Type = type;
			this.section.PropertySettings[name].AllowAnonymous = allowAnonymous;
			this.section.PropertySettings[name].CustomProviderData = customProviderData;
			this.Save();
		}
		public void RemoveProperty(String name)
		{
			this.section.PropertySettings.Remove(name);
			this.Save();
		}
		public void ClearPropertys()
		{
			this.section.PropertySettings.Clear();
			this.Save();
		}
		public ProfilePropertySettings GetProperty(String name)
		{
			return this.section.PropertySettings[name];
		}
		public RootProfilePropertySettingsCollection GetProperties()
		{
			return this.section.PropertySettings;
		}

		public void AddGroup(String name)
		{
			this.section.PropertySettings.GroupSettings.Add(new ProfileGroupSettings(name));
			this.Save();
		}
		public void RemoveGroup(String name)
		{
			this.section.PropertySettings.GroupSettings.Remove(name);
			this.Save();
		}
		public void ClearGroup()
		{
			this.section.PropertySettings.GroupSettings.Clear();
			this.Save();
		}
		public ProfileGroupSettings GetGroup(String name)
		{
			return this.section.PropertySettings.GroupSettings[name];
		}
		public ProfileGroupSettingsCollection GetGroups()
		{
			return this.section.PropertySettings.GroupSettings;
		}

		public void AddGroupProperty(String groupName, String name, String type, String provider, SerializationMode serializeAs, Boolean allowAnonymous, String defaultValue, Boolean readOnly,	 String customProviderData)
		{
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings.Add(new ProfilePropertySettings(name, readOnly, serializeAs, provider, defaultValue, type, allowAnonymous, customProviderData));
			this.Save();
		}
		public void ModifyGroupProperty(String groupName, String name, String type, String provider, SerializationMode serializeAs, Boolean allowAnonymous, String defaultValue, Boolean readOnly, String customProviderData)
		{
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name].ReadOnly = readOnly;
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name].SerializeAs = serializeAs;
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name].Provider = provider;
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name].DefaultValue = defaultValue;
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name].Type = type;
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name].AllowAnonymous = allowAnonymous;
			this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name].CustomProviderData = customProviderData;
			this.Save();
		}
		public void RemoveGroupProperty(String groupname, String name)
		{
			this.section.PropertySettings.GroupSettings[groupname].PropertySettings.Remove(name);
			this.Save();
		}
		public void ClearGroupPropertys(String groupname)
		{
			this.section.PropertySettings.GroupSettings[groupname].PropertySettings.Clear();
			this.Save();
		}
		public ProfilePropertySettings GetGroupProperty(String groupName, String name)
		{
			if (this.section.PropertySettings.GroupSettings[groupName] != null)
			{
				return this.section.PropertySettings.GroupSettings[groupName].PropertySettings[name];
			}

			return null;
		}
		public ProfilePropertySettingsCollection GetGroupProperties(String groupName)
		{
			if (this.section.PropertySettings.GroupSettings[groupName] != null)
			{
				return this.section.PropertySettings.GroupSettings[groupName].PropertySettings;
			}

			return null;
		}

		public void AddProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Add(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ModifyProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Modify(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void RemoveProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Remove(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ClearProviders()
		{
			this.section.Providers.Clear();

			this.Save();
		}
		public RequiredConnectionStringNameProviderSettings GetProvider(String name)
		{
			return RequiredConnectionStringNameProviderSettings.ConvertToRequiredConnectionStringNameProviderSettings(
				this.section.Providers[name]
			);
		}
		public Collection<RequiredConnectionStringNameProviderSettings> GetProviders()
		{
			Collection<RequiredConnectionStringNameProviderSettings> providers = new Collection<RequiredConnectionStringNameProviderSettings>();

			foreach (ProviderSettings provider in this.section.Providers)
			{
				providers.Add(
					RequiredConnectionStringNameProviderSettings.ConvertToRequiredConnectionStringNameProviderSettings(provider)
				);
			}

			return providers;
		}
	}

	public sealed class ProfileSectionManagerForSql : ConfigurationManagerBase
	{
		private ProfileSection section;

		public ProfileSectionManagerForSql()
		{
			this.section = (ProfileSection)base.GetConfigurationSection("system.web/profile");
		}

		public void AddProvider(SqlProfileProviderSettings provider)
		{
			SqlProfileProviderSettings.Add(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ModifyProvider(SqlProfileProviderSettings provider)
		{
			SqlProfileProviderSettings.Modify(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void RemoveProvider(SqlProfileProviderSettings provider)
		{
			SqlProfileProviderSettings.Remove(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ClearProviders()
		{
			this.section.Providers.Clear();

			this.Save();
		}
		public SqlProfileProviderSettings GetProvider(string name)
		{
			return SqlProfileProviderSettings.ConvertToSqlProfileProviderSettings(
				this.section.Providers[name]
			);
		}
		public Collection<SqlProfileProviderSettings> GetProviders()
		{
			Collection<SqlProfileProviderSettings> providers = new Collection<SqlProfileProviderSettings>();

			foreach (ProviderSettings provider in this.section.Providers)
			{
				providers.Add(
					SqlProfileProviderSettings.ConvertToSqlProfileProviderSettings(provider)
				);
			}

			return providers;
		}
	}
}